﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.IO;
using System.Xml;

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo dir = new DirectoryInfo(@"C:\Users\e.cornely\Documents\Visual Studio 2010\Projects\ObjectFeatures\WpfAlarmTimer\Resources\");
            foreach (FileInfo fi in dir.EnumerateFiles("*.xaml"))
            {
                string vbName = fi.Name.Replace(".xaml", "");

                XmlDocument doc = new XmlDocument();
                using (FileStream inStream = fi.OpenRead()) {
                    doc.Load(inStream);
                    inStream.Close();
                }
                XmlAttribute c = doc.CreateAttribute("x", "Class", "http://schemas.microsoft.com/winfx/2006/xaml");
                c.Value = "WpfAlarmTimer.Resources." + vbName;
                doc.DocumentElement.Attributes.Append(c);
                using (FileStream xmlOut = fi.Open(FileMode.Create)) {
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Encoding = Encoding.UTF8;
                    settings.OmitXmlDeclaration = false;
                    settings.IndentChars = "    ";
                    settings.NewLineChars= "\r\n";
                    settings.Indent = true;
                    XmlWriter writer = XmlWriter.Create(xmlOut, settings);
                    doc.WriteTo(writer);
                    writer.Flush();
                    xmlOut.Close();
                }

                var unit = new CodeCompileUnit();
                unit.ReferencedAssemblies.Add("System");
                unit.ReferencedAssemblies.Add("System.Windows.Controls");
                var ns = new CodeNamespace("WpfAlarmTimer.Resources");
                var t = new CodeTypeDeclaration(vbName);
                t.IsPartial = true;
                t.IsClass = true;
                t.BaseTypes.Add(new CodeTypeReference("System.Windows.Controls.Viewbox"));
                var ctor = new CodeConstructor();
                ctor.Attributes = MemberAttributes.Public;
                var ctorEx = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "InitializeComponent", new CodeExpression[] { });
                ctor.Statements.Add(ctorEx);
                t.Members.Add(ctor);
                ns.Types.Add(t);
                unit.Namespaces.Add(ns);
                var provider = new CSharpCodeProvider();
                using (Stream outStream = new FileStream(fi.FullName+".cs", FileMode.Create))
                {
                    var tw = new StreamWriter(outStream);
                    var sw = new IndentedTextWriter(tw);
                    provider.GenerateCodeFromCompileUnit(unit, sw, null);
                    sw.Flush();
                }
            }
            Console.ReadKey(false);
        }
    }
}
